package com.aliang.shopping.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aliang.shopping.config.OssConfig;
import com.aliang.shopping.mapper.MessageMapper;
import com.aliang.shopping.mapper.UserInfoMapper;
import com.aliang.shopping.model.converter.ChatConverter;
import com.aliang.shopping.model.po.Message;
import com.aliang.shopping.model.vo.chat.MessageVO;
import com.aliang.shopping.model.vo.chat.SimpleContactVO;
import com.aliang.shopping.security.auth.UserHolder;
import com.aliang.shopping.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ShiBaLover
 * @description 针对表【message(消息表)】的数据库操作Service实现
 * @createDate 2024-10-28 09:35:47
 */
@Service
@AllArgsConstructor
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
        implements MessageService {

    private final UserInfoMapper userInfoMapper;
    private final ChatConverter chatConverter;
    private final OssConfig ossConfig;

    @Override
    public List<MessageVO> getMessageListByContactId(Long contactId) {
        // 获取当前登录用户信息
        Long userId = UserHolder.getUserId();
        List<Message> list = this.baseMapper.getMessageListByContactId(userId, contactId);
        if (ObjectUtil.isEmpty(list)) {
            return null;
        }
        Set<Long> userIds = new HashSet<>();
        list.forEach(item -> {
            userIds.add(item.getFromId());
            userIds.add(item.getToId());
        });
        // 获取联系人头像数据
        List<SimpleContactVO> simpleContactVOS = this.userInfoMapper.getAvatarById(userIds);
        Map<Long, String> avatarMap = simpleContactVOS.stream().collect(Collectors.toMap(SimpleContactVO::getId,
                item -> {
                    if (StrUtil.isNotBlank(item.getAvatar()))
                        return ossConfig.getUrl() + item.getAvatar();
                    return item.getAvatar();
                }));

        List<MessageVO> voList = chatConverter.toMessageVOList(list);
        voList.forEach(item -> {
            //设置头像
            SimpleContactVO from = item.getFrom();
            SimpleContactVO to = item.getFrom();
            from.setAvatar(avatarMap.get(from.getId()));
            to.setAvatar(avatarMap.get(to.getId()));
            //设置图片路径
            if (Objects.equals(item.getType(), "image")) {
                item.setContent(ossConfig.getUrl() + item.getContent());
            }
        });
        //返回封装的数据
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBYUserIdAndContactId(Long userId, Long contactId) {
        //  根据用户Id和联系人Id获取信息的id、删除人
        List<Message> list = this.baseMapper.queryBYUserIdAndContactId(userId, contactId);
        //获取需要修改的信息
        if (list.isEmpty()) {
            return;
        }
        List<Message> collect = list.stream().filter(item -> Objects.isNull(item.getDeleteId()))
                .collect(Collectors.toList());
        if (!collect.isEmpty()) {
            collect.forEach(item -> item.setDeleteId(userId));
            this.updateBatchById(collect);
        }
        List<Message> collect1 = list.stream().filter(item -> Objects.nonNull(item.getDeleteId()))
                .collect(Collectors.toList());
        //删除的信息
        if (!collect1.isEmpty()) {
            this.removeByIds(collect1.stream().map(Message::getId).collect(Collectors.toList()));
        }

    }
}




